package com.example.coffeeseller_2.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by hfs on 2017/5/9.
 */
public class CountDownBackView extends FrameLayout {

    //private static final int backgroundClolr = Color.parseColor("#a0522d");
    //private static final float backgroudRadius = 100;
    private CountDownText countDownText;
    private CompleteListener completeListener;

    public CountDownBackView(Context context) {
        this(context,null);
    }

    public CountDownBackView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CountDownBackView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void init(Context context) {
        //setBackground(backgroundClolr,backgroudRadius);
        //setPadding(0,0,0,0);
        countDownText  = new CountDownText(context);
        countDownText.setText(getCountDownText(countDownText.getMillisInFuture()/1000));
        //setCompoundDrawablesLeft(countDownText,R.mipmap.ic_chevron_left_white_24dp,0);
        countDownText.setOnIntervalListener(new CountDownText.IntervalListener() {
            @Override
            public void onTick(long millisUntilFinished) {
                countDownText.setText(getCountDownText(millisUntilFinished));
            }

            @Override
            public void onFinish() {
                countDownText.setText(getCountDownText(0));
                if(completeListener != null){
                    completeListener.onFinish();
                }
            }
        });

        /*FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER;
        countDownText.setLayoutParams(lp);*/
        countDownText.setGravity(Gravity.CENTER);
        addView(countDownText);
    }
    private String getCountDownText(long count){
        return "返回 ("+count+"s)";
    }
    public CountDownText getCountDownTextView() {
        return countDownText;
    }
    public void startCount(){
        if(countDownText != null){
            countDownText.startCount();
        }
    }
    public void stopCount(){
        if(countDownText != null){
            countDownText.stopCount();
        }
    }
    public void setTime(int time){
        countDownText.setMillisInFuture(time*1000);
        countDownText.setText("返回("+time+"s)");
    }
    public interface CompleteListener{
        void onFinish();
    }
    public void setOnCompleteListener(CompleteListener listener){
        completeListener = listener;
    }

    private void setCompoundDrawablesLeft(TextView tv, int resId,int padding) {
        Drawable drawable = tv.getContext().getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv.setCompoundDrawables(drawable, null, null, null);
        tv.setCompoundDrawablePadding(padding);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setBackground(int backgroundClolr,float backgroudRadius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(backgroudRadius);
        gradientDrawable.setColor(backgroundClolr);
        setBackground(gradientDrawable);
    }

}

